//
//  EVSimpleAlertContentView.m
//  EVClub
//
//  Created by Eric on 16/8/1.
//  Copyright © 2016年 BitRice. All rights reserved.
//

#import "EVSimpleAlertContentView.h"
#import "EVAlertView.h"
@implementation EVSimpleAlertContentView
+ (instancetype)instanceWithTitle:(NSString *)title detail:(NSString *)detail commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton{
    return [self instanceWithTitle:title detail:detail commitButton:commitButton cancelButton:cancelButton NibName:@"EVSimpleAlertContentView"];
}
+ (instancetype)instanceInputViewWithTitle:(NSString *)title commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton{
    EVSimpleAlertContentView *view = [self instanceWithTitle:title detail:nil commitButton:commitButton cancelButton:cancelButton NibName:@"EVSimpleAlertInputView"];
    view.isInputAlertView = YES;
    return view;
}
+ (instancetype)instanceWithTitle:(NSString *)title detail:(NSString *)detail commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton NibName:(NSString*)nibName
{
    EVSimpleAlertContentView *view = nil;
    NSArray *xibObjects = [[UINib nibWithNibName:nibName bundle:nil]instantiateWithOwner:self options:nil];
    if(xibObjects.count > 0){
        if ((view = (EVSimpleAlertContentView*)xibObjects[0])) {
            view.titleLabel.text = title;
            if(!title) view.detailTopConstraint.constant = 0;
            view.detailLabel.text = detail;
            if(!detail) view.detailBottomConstraint.constant = 0;
            [view.commitButton setTitle:commitButton forState:UIControlStateNormal];
            [view.cancelButton setTitle:cancelButton forState:UIControlStateNormal];
            if(!commitButton || !cancelButton){
                view.oneButtonConstratnt.priority = UILayoutPriorityDefaultHigh + 1;//变成一个按钮时的样子
                view.sepLineView.hidden = YES;
            }
            view.inputTF.clickDelegate = view;
            view.cancelButton.hidden = !cancelButton;
            view.commitButton.hidden = !commitButton;
            [view setNeedsLayout];
            [view layoutIfNeeded];
        }
    }
    return view;
    
}
-(void)FinishButtonDidPressed:(EVTextField*)textField{
    [self onCommitButtonTouched:_commitButton];
}
-(void)CancelButtonDidPressed:(EVTextField*)textField{
    [self onCancelButtonTouched:_cancelButton];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    if(!self.inputTF.coverManagerView){
        self.inputTF.coverManagerView = self.superview;
    }
    self.inputTF.hidden = _isInputAlertView? NO : YES;
    self.detailLabel.hidden = _isInputAlertView? YES : NO;
}
-(void)setIsInputAlertView:(BOOL)isInputAlertView{
    _isInputAlertView = isInputAlertView;
}
#pragma mark - Actions
- (IBAction)onCancelButtonTouched:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(TDAlertView:didDismissWithButtonIndex:)]) {
        [self.delegate TDAlertView:nil didDismissWithButtonIndex:EVAlertViewButtonIndexCancel];
    }
}

- (IBAction)onCommitButtonTouched:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(TDAlertView:didDismissWithButtonIndex:)]) {
        [self.delegate TDAlertView:nil didDismissWithButtonIndex:EVAlertViewButtonIndexCommit];
    }
}

- (IBAction)onCloseButtonTouched:(id)sender {
    [self onCancelButtonTouched:sender];
}
@end
